home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1263 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. Path: cdf.toronto.edu!a228lave
  3. From: a228lave@cdf.toronto.edu (Laventure Derek)
  4. Subject: getchar() problems!
  5. Message-ID: <DL2wFD.8BK@cdf.toronto.edu>
  6. Sender: news@cdf.toronto.edu (Usenet News)
  7. Nntp-Posting-Host: sat
  8. Organization: University of Toronto Computing Disciplines Facility
  9. Date: Fri, 12 Jan 1996 17:25:59 GMT
  10.  
  11. Hello!
  12.  
  13. I am having some trouble with the following code.  All I want it to do is
  14. read characters from the keyboard until the user types a # sign, at which point
  15. I want to stop accepting input, and tell the user some info.  Here it is:
  16.  
  17. #include <stdio.h>
  18.  
  19. #define SPACE ' '
  20. #define NL '\n'
  21.  
  22. int main(void)
  23. {
  24.    char ch;
  25.    int sp_c, nl_c, ot_c;   /* Space count, Newline count and other count */     
  26. sp_c=0;
  27.    nl_c=0;
  28.    ot_c=0;
  29.  
  30.    printf("Please enter a series of chars.  Type '#' to quit.\n");
  31.    while ((ch=getchar()) != '#')
  32.       {
  33.          switch (ch)
  34.             {
  35.                case SPACE: sp_c++; break;
  36.                case NL   : nl_c++; break;
  37.                default   : ot_c++;
  38.             }
  39.       }
  40.    printf("There were %d spaces, %d newlines, and %d other                      
  41. chars!",sp_c,nl_c,ot_c);
  42.    ch=getchar();
  43. }
  44.  
  45. }
  46.  
  47. What _actually_ happens is the code takes characters until you hit the #, and 
  48. then continues until you press return.  Also, the final ch=getchar(); line
  49. appears to be ignored... it's intended as a pause.  I'm somewhat of a beginner
  50. to the language, so any help will be greatly appreciated!
  51.  
  52. Thanx
  53.  
  54. Derek Laventure
  55. a228lave@cdf.toronto.edu
  56.